﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace House_World
{
    class Terrain
    {
        public VertexPositionTexture[] verts;
        public BasicEffect effect;
        public Texture2D texDestiny;
        public Texture2D texOrigin;
        public Effect effects;
        float time;
        bool sortTexture;
        Matrix world, view, projection;
        public Terrain(GraphicsDevice graphicsDevice, Camera camera, Texture2D texture,ContentManager Content)
        {
            time = 0;
            sortTexture = true;
            effect = new BasicEffect(graphicsDevice);
            world = Matrix.Identity;
            effect.World = world;

            view= camera.View;
            effect.View = view;

            texOrigin = texture;
            projection = camera.Projector;
            effect.Projection = projection;

            effect.TextureEnabled = true;
            effect.Texture = texture;
            effects = Content.Load<Effect>(@"Effect\morph");
            verts = new[]
            {
                new VertexPositionTexture(new Vector3(0,0.3f,-30),new Vector2(0.5f,0)),
                new VertexPositionTexture(new Vector3(32,0.3f,-30),new Vector2(0,1)),
                new VertexPositionTexture(new Vector3(0,-0.3f,0),new Vector2(1,1)),
                new VertexPositionTexture(new Vector3(32,-0.3f,0),new Vector2(0.5f,0))
            };
        }
        public void Update(GameTime gameTime)
        {
            time += gameTime.ElapsedGameTime.Milliseconds * 0.001f;
            if (time >= 1.0f)
            {
                time = 0;
                this.sortTexture = !this.sortTexture;

            }
        }
        public void Draw(GraphicsDevice graphicsDevice,VertexBuffer vertexBuffer,Texture2D destiny,Camera camera)
        {
            texDestiny = destiny;
            effects.CurrentTechnique=effects.Techniques["Technique1"];
            effects.Parameters["World"].SetValue(world);
            effects.Parameters["View"].SetValue(camera.View);
            effects.Parameters["Projection"].SetValue(camera.Projector);
            effects.Parameters["t"].SetValue(this.time);
            if (sortTexture)
            {
                effects.Parameters["texOrigin"].SetValue(texOrigin);
                effects.Parameters["texDestiny"].SetValue(texDestiny);
            }
            else
            {
                effects.Parameters["texOrigin"].SetValue(texDestiny);
                effects.Parameters["texDestiny"].SetValue(texOrigin);
            }
                foreach (EffectPass pass in effects.CurrentTechnique.Passes)
                {
                    pass.Apply();

                    graphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, verts, 0, 2);

                }
            
        }
    }
}
